<script type="text/javascript">
	$(document).ready(function(){
		

		var form_action = "<?php echo $action; ?>";

		// Create jqxNumberInput
        $("#player_weight_input,#player_height_input").jqxNumberInput({ width: '100px', height: '25px', inputMode: 'simple', spinButtons: true });
		
		if(form_action == "add"){
			//$("#name_label,#country_label").addClass("required");
		}else if(form_action == "edit"){
			//$("#name_label,#country_label").addClass("required");
			
			$("#player_id").val("<?php echo isset($player)?$player[0]->playerid:""; ?>");
			$("#player_name").val("<?php echo isset($player)?$player[0]->name:""; ?>");
			$("#player_no").val("<?php echo isset($player)?$player[0]->no:""; ?>");
			$("#player_position").val("<?php echo isset($player)?$player[0]->position:""; ?>");
			$("#player_gender").val("<?php echo isset($player)?$player[0]->gender:""; ?>");
			$("#player_birth").val("<?php echo isset($player)?$player[0]->birth:""; ?>");
			$("#player_team").val("<?php echo isset($player)?$player[0]->teamid:""; ?>");
			
			$("#player_height").val("<?php echo isset($player)?$player[0]->height:""; ?>");
			$("#player_weight").val("<?php echo isset($player)?$player[0]->weight:""; ?>");
			$("#player_height_input").val("<?php echo isset($player)?$player[0]->height:""; ?>");
			$("#player_weight_input").val("<?php echo isset($player)?$player[0]->weight:""; ?>");
			$("#player_picture").val("<?php echo isset($player)?$player[0]->picture:""; ?>");
			
			
			$enabled = "<?php echo isset($player)?$player[0]->enabled:""; ?>";
			if($enabled == "1"){
				$("#player_enabled").attr("checked","checked");				
			}else{
				$("#player_enabled").removeAttr("checked");
			}
		}else{
			alert("Operation Invalid.");
		}

		$("#save").on('click', function (event) {
			var url = "<?php echo base_url(); ?>index.php/player/";

			if (form_action == "add"){
				url += "doCreate";
			}else if(form_action == "edit"){
				url += "doEdit";
			}

			 var form_data = $('form').serialize();
			
			var request = $.ajax({
				url: url,
				type: "POST",			
			    async: false,
		        dataType: "html",
		        data: form_data
			});

			request.done(function(msg) {				
				if (form_action == "add"){
					$("#player_name").val("");
					$("#player_no").val("0");
					$("#player_position").val("1B");
					$("#player_gender").val("Male");
					$("#player_birth").val("");
					
					$("#player_height").val("0.00");
					$("#player_weight").val("0.00");
					$("#player_height_input").val("0.00");
					$("#player_weight_input").val("0.00");
					$("#player_picture").val("");
					$("#player_enabled").attr("checked","checked");			
				}
					
				alert("Data Saved");		
			});

			request.fail(function(jqXHR, textStatus) {
				alert( "Request failed: " + textStatus );
			});
		});
		

		$("#back").on('click', function (event) {
			window.location = "<?php echo base_url(); ?>index.php/player";
		});

		 $('#player_birth').datepicker({
             showOn: "button",
             //buttonImage: "/Content/images/calendar.gif",
             buttonImageOnly: false,
             changeMonth: true,
             changeYear: true,
             dateFormat: 'yy-mm-dd',
             onSelect: function () {
                 $(this).select();
             }
         });


         $('#player_weight_input').on('valuechanged', function (event) {
             var value = event.args.value;
             $('#player_weight').val(value);
         });

         $('#player_height_input').on('valuechanged', function (event) {
             var value = event.args.value;
             $('#player_height').val(value);
         });

       //For Excel file upload
         var test = $("#userfile").makeAsyncUploader({
             upload_url: "<?php echo base_url(); ?>index.php/common/doUpload?page=players", // Important! This isn't a directory, it's a HANDLER such as an ASP.NET MVC action method, or a PHP file, or a Classic ASP file, or an ASP.NET .ASHX handler. The handler should save the file to disk (or database).
             flash_url: '<?php echo base_url(); ?>asset/js/jquery.asyncupload/swfupload.swf',
             button_image_url: '<?php echo base_url(); ?>asset/js/jquery.asyncupload/blankButton.png',
             file_types: '*.jpg;*.png',
             file_types_description: 'All Picture Files',
             file_size_limit: '10 MB',
             disableDuringUpload: 'INPUT[type="submit"]',
             button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT
         });
			
	});
</script>
<img id="top" src="top.png" alt="">
<div id="form_container">


	<form id="form_889069" class="appnitro" method="post" action="">
		<div class="form_description">
			<h2><?php echo $title; ?></h2>
		</div>
		<ul>

			<li id="li_1"><label class="description" id="name_label">Name</label>
				<div>
					<input id="player_name" name="player_name"
						class="element text medium" type="text" maxlength="200" value="" />
				</div></li>
			<li id="li_2"><label class="description" id="team_label">Team</label>
				<div>
						<select id="player_team" name="player_team">
						<?php 
		if(isset($teams)){
			
			foreach($teams as $team){
				echo '<option value="'.$team->teamid.'">'.$team->team_name.'</option>"';
			}
		}
	?>										
						</select> 
				</div></li>
		
			<li id="li_2"><label class="description" id="no_label">No.</label>
				<div>
						<select id="player_no" name="player_no">
									<?php for($i=0;$i<100;$i++){
										echo '<option value="'.$i.'">'.$i.'</option>"';
										
									} ?>
						</select> 
				</div></li>
			<li id="li_2"><label class="description" id="position_label">Position</label>
				<div>
					<select id="player_position" name="player_position">
						<option value="1B" selected>1B</option>
						<option value="2B">2B</option>
						<option value="3B">3B</option>
						<option value="SS">SS</option>
						<option value="P">P</option>
						<option value="C">C</option>
						<option value="LF">LF</option>
						<option value="CF">CF</option>
						<option value="RF">RF</option>
						</select>						
				</div>
			</li>
			<li id="li_2"><label class="description" id="gender_label">Gender</label>
				<div>
					<select id="player_gender" name="player_gender">
						<option value="Male" selected>Male</option>
						<option value="Female">Female</option>
						</select>						
				</div>
			</li>
			<li id="li_2"><label class="description" id="birth_label">Birth</label>
				<div>
						<input id="player_birth" name="player_birth"
						class="element text small" type="text" maxlength="10" value="" />
				</div></li>
			<li id="li_2"><label class="description" id="weight_label">Weight</label>
				<div>
						<div id="player_weight_input" name="player_weight"
						class="element text small" />
						<input type="hidden" id="player_weight" value="" /> 
				</div></li>
				<li id="li_2"><label class="description" id="height_label">Height</label>
				<div>
						<div id="player_height_input" name="player_height"
						class="element text small"  />
						<input type="hidden" id="player_height" value="" />
				</div></li>
				
				<li id="li_1"><label class="description" id="picture_label">Picture</label>
				<div>
					<input id="userfile" name="userfile"
						class="element file" type="file" value="" />
						<input type="hidden" id="player_picture" name="player_picture" value="" />
				</div>
				</li>
			
			<li id="li_3"><label class="description" for="element_3"> </label> <span>
					<input id="player_enabled" name="player_enabled"
					class="element checkbox " type="checkbox" value="1"
					checked="checked" /> <label class="choice" for="element_3_1">Enabled</label>

			</span></li>

			<li class="buttons"><input type="hidden" id="player_id"
				name="player_id" value="" /> <input type="button" value="Save"
				id="save" class="button_text" /> <input type="button" value="Back"
				id="back" class="button_text" /></li>
		</ul>
	</form>
	
</div>
